// pages/pay/pay.js
const HTTP = require('../../utils/util');
const common = require('../../libs/common');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page:1,
        limit:10,
        order_id: '',
        isPay: false,
        shop_id: '',
        money: '',
        isOk: false,
        shopList:[]
    },
    goRemark() {
        wx.navigateTo({
            url: '../remark/remark?id=' + this.data.order_id,
        });
    },
    newMoney() {
        this.setData({
            isOk: false
        });
    },
    isMoney() {
        if (common.checkMoney(this.data.money)) {
            this.setData({
                isOk: true
            });
        }
    },
    moneyChange(e) {
        console.log(e.detail.value);
        this.setData({
            money: e.detail.value
        });
    },
    createOrder() {
        if (this.data.isOk) {
            HTTP.getData('/api/order/createOrder', {
                shop_id: this.data.shop_id,
                money: this.data.money
            }, res => {
                console.log(res);
                this.setData({
                    order_id: res.data
                });
                this.pay(res.data)
            });
        } else {
            common.checkMoney(this.data.money);
            return false;
        };
    },
    pay(id) {
        HTTP.getData('/api/order/wxPay', {
            order_id: id
        }, res => {
            console.log(res);
            wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: 'MD5',
                paySign: res.data.paySign,
                success:res=>{
                    console.log(res)
                    this.setData({
                        isPay:true
                    });
                    this.getPayOkShop();
                },
                fail:res=>{
                    console.log(res);
                },
            });        
        });
    },
    getPayOkShop(){
        HTTP.getData('/api/order/paySuccess',{
            page:this.data.page,
            limit:this.data.limit
        },res=>{
            common.handlePageChange(this.data.page,res.data.data,this.data.shopList,(arr)=>{
                this.setData({
                    shopList:arr
                });
            },'商铺');
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.id);
        this.setData({
            shop_id: options.id
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.isPay){
            this.setData({
                page:this.data.page+1
            });
            this.getPayOkShop();
        }else{
            return false;
        }
        
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})